<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Logs_m extends MY_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_logs_by_id($id){

        $this->db->select('*');
        $this->db->from('logs');
        $this->db->where('id',$id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改日志信息
     */
    public function sava_logs($id,$data){

        $this->db->where('id',$id);
        $this->db->update('logs', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增日志
     */
    public function add_logs($uid,$log){

        $data['uid'] = $uid;
        $data['t'] = time();
        $data['ip'] = $_SERVER["REMOTE_ADDR"];
        $data['log'] = $log;
        $this->db->insert('logs',$data);
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有日志
     */
    public function get_all_logs($start,$limit,$user_id="0"){
        $this->db->select('*');
        $this->db->from('logs');
        $this->db->limit($limit,$start);
        if(!empty($user_id)){
            $this->db->where('uid',$user_id);
        }
        $this->db->order_by('id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_logs_by_uid($uid){
        $sql = "SELECT * FROM {$this->db->dbprefix}logs  where uid = $uid ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除日志
     */
    public function delete_logs_by($where){
        $this->db->where($where)->delete('logs');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


    public function count_goods_list_by_catagory_id($category_tid){
        $this->db->select('id');
        $this->db->from('logs');
        $this->db->where('good_category',$category_tid);
        $this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    /*喜欢频道*/
    public function get_goods_list_by_catagory_id($page=0,$limit=4,$category_tid){
        $start = $page*$limit;
        $query = $this->db->select('*');
        $this->db->from('logs');
        $this->db->where('good_category',$category_tid);
        $this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
        $this->db->limit($limit,$start);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function is_favorites($user_id,$category_tid){
        $query = $this->db->select('*');
        $this->db->from('user_categorys');
        $this->db->where('user_id',$user_id);
        $this->db->where('category_tid',$category_tid);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }        
    }
    public function add_user_categorys($data){
        return $this->db->insert('user_categorys',$data);

    }
    public function updates_user_categorys_catogory_view($id){
        $sql = "update {$this->db->dbprefix}user_categorys set catogory_view=catogory_view +1 where category_id = ".$id;
        $query=$this->db->query($sql);
    }
    //根据搜索用户名的关键字查找日志
    public function get_goods_by_keyword($keywords,$order,$start,$limit){
        $query = $this->db->select('*');
        $this->db->from('logs');
        $this->db->like('good_title', $keywords, 'both');
        $this->db->limit($limit,$start);
        $this->db->order_by('good_add_time',$order);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //计算搜索的关键字
    public function get_total_goods_by_keyword($keywords){
        $this->db->select('id');
        $this->db->from('logs');
        $this->db->like('good_title', $keywords, 'both');
        $total=$this->db->count_all_results();
        return $total;
    }
    //查找所有参与免单的日志
    public function get_all_vouchers_goods($start,$limit){
        $this->db->select('*')->from('logs')->where('good_voucher',1)->limit($limit,$start);
        $this->db->order_by('id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //计算所有的参与免单的日志
    public function count_all_vouchers_good(){
        $this->db->select('id');
        $this->db->from('logs');
        $this->db->where('good_voucher',1);
        $total=$this->db->count_all_results();
        return $total;
    }
    //删除日志
    public function delete_logs_by_id($id){
        $this->db->where('id',$id)->delete('logs');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
}
